﻿var acceptClick;
var keyValue = request('keyValue');
// 设置权限
var setAuthorize;
// 设置表单数据
var setFormData;
// 验证数据是否填写完整
var validForm;
// 保存数据
var save;

var isUpdate = false;


//单据数据
var billData = {};
//行程数据
var routes = [];
//支出事项
var payitemIds = "";



var reloadRouteCallBack;
var reloadPayitemCallBack;
var reloadPayitemFormOptionCallBack;
var reloadCashSupplierCallBack;
var reloadCivilServiceCardCallBack;
var reloadTransferSupplierCallBack;

var bootstrap = function ($, learun) {
    "use strict";

    var userinfo = learun.clientdata.get(['userinfo']);
    var _currentPayitemFromObj; //当前支出事项标准选项

    // 设置权限
    setAuthorize = function (data) {
        //禁用输入框自动填充
        $("input[type='text']").each(function (index) {
            this.autocomplete = "off";
        });
    };

    var page = {
        init: function () {
            $('.lr-form-wrap').lrscroll();
            page.bind();

            loadFlow(5, ["填写基本信息","所需资金报销明细","上传报销发票和附件","结算方式","要素确认和退回处理"]);

            page.initData();
        },
        bind: function () {
            // 部门选择
            $('#departmentId').lrselect({
                type: 'default',
                url: config.base_server() + 'departments/tree?companyId='+userinfo.user.companyId + '&access_token=' + config.getToken().access_token,
                placeholder: '部门',
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {
                }
            });

            //报销人
            $("#userid").lrformselect({
                layerUrl: config.webportal_server + 'pages/system/user/selectOnlyForm.html?companyId=' + userinfo.user.companyId + '&access_token=' + config.getToken().access_token,
                layerUrlW: 400,
                layerUrlH: 300,
                dataUrl: config.base_server() + 'users/getNames?access_token=' + config.getToken().access_token,
                select: function (item) {
                }
            }).lrformselectSet(userinfo.user.id);

            //选择关联单据
            $('#btn_reference').on('click', function () {
                learun.layerForm({
                    id: 'selectForm',
                    title: '选择报销单',
                    url: config.webportal_server + 'pages/bill/selectForm.html?categoryId=1&schemeCode=bill_other1&access_token=' + config.getToken().access_token,
                    width: 800,
                    height: 500,
                    callBack: function (id) {
                        return top[id].acceptClick(referenceSelect);
                    }
                });
            });

            // 选择支出事项
            $('#lnkSelectQE').on('click', function () {
                learun.layerForm({
                    id: 'form_payitem',
                    title: '选择支出事项',
                    url: config.webportal_server + 'pages/base/payitem/multi_select.html?selectValue='+ payitemIds +'&access_token=' + config.getToken().access_token,
                    width: 450,
                    height: 780,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadPayitemCallBack);
                    }
                });
            });

            // 新增行程
            $('#lnkTravelAdd').on('click', function () {
                learun.layerForm({
                    id: 'form_route',
                    title: '添加行程',
                    url: config.webportal_server + 'pages/bill3/component/route_form.html?ccess_token=' + config.getToken().access_token,
                    height: 870,
                    width: 1200,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadRouteCallBack);
                    }
                });
            });

            //选择结算方式
            $("input[name='settlement']").change(function() {
                var id = $(this).attr("id");
                switch(id) {
                    case "_settlement1":
                        if ($(this).is(':checked')) {
                            $("#pnlCheque").show();
                            $("#lstTrSettlement1").show();
                        } else {
                            $("#pnlCheque").hide();
                            $("#lstTrSettlement1").hide();
                        }
                        break;
                    case "_settlement2":
                        if ($(this).is(':checked')) {
                            $("#lstTrSettlement2").show();
                            $("#lstTrSettlement2").on("change", function (e) {
                               var value = $(this).val();
                               autoCalcSettlement();
                               $(this).val($.formatMoney(value, 2));
                            });
                        } else {
                            $("#lstTrSettlement2").hide();
                        }
                        break;
                    case "_settlement3":
                        if ($(this).is(':checked')) {
                            $("#pnlCivilServiceCard").show();
                            $("#lstTrSettlement3").show();
                        } else {
                            $("#pnlCivilServiceCard").hide();
                            $("#lstTrSettlement3").hide();
                        }
                        break;
                    case "_settlement4":
                        if ($(this).is(':checked')) {
                            $("#pnlTransfer").show();
                            $("#lstTrSettlement4").show();
                        } else {
                            $("#pnlTransfer").hide();
                            $("#lstTrSettlement4").hide();
                        }
                        break;
                }
            });

            // 新增支票收款人信息
            $('#btnCheque').on('click', function () {
                learun.layerForm({
                    id: 'form_cheque',
                    title: '选择转账供应商',
                    url: config.webportal_server + 'pages/base/supplier/multi_select.html?ccess_token=' + config.getToken().access_token,
                    height: 870,
                    width: 1200,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadCashSupplierCallBack);
                    }
                });
            });

            // 新增公务卡结算信息
            $('#btnCivilServiceCard').on('click', function () {
                learun.layerForm({
                    id: 'form_civilServiceCard',
                    title: '选择公务卡信息',
                    url: config.webportal_server + 'pages/bill3/component/person.html?access_token=' + config.getToken().access_token,
                    height: 670,
                    width: 1024,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadCivilServiceCardCallBack);
                    }
                });
            });

            // 新增转账信息
            $('#btnTransfer').on('click', function () {
                learun.layerForm({
                    id: 'form_transfer',
                    title: '选择转账供应商',
                    url: config.webportal_server + 'pages/base/supplier/multi_select.html?ccess_token=' + config.getToken().access_token,
                    height: 870,
                    width: 1200,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadTransferSupplierCallBack);
                    }
                });
            });


            //TODO 提交
            $('#btnok').on('click', function () {
                alert(getBillData());
            });
        },

        initData: function () {
            var currentDate = new Date();
            var year = currentDate.getFullYear();
            var month = ("0" + (currentDate.getMonth() + 1)).slice(-2);
            var day = ("0" + currentDate.getDate()).slice(-2);
            $("#billDate").val(year + "-" + month + "-" + day);

            $("#createUsername").val(userinfo.user.realName);
            $('#departmentId').lrselectSet(userinfo.user.departmentId);
        },

        //绘制行程图
        redrawRoute: function (route, index) {
            var $outHtml = "";
               $outHtml += '  <div class="nk-receipt-panel" style="margin-bottom: 80px; display: block;" id="route_div_'+route.billSort+'" data-index="'+index+'" data-includeMeal="'+route.includeMeal+'" data-includeHotel="'+route.includeHotel+'">';
               $outHtml += '     <div class="nk-receipt-panel" id="pnlTravelTitle" n="标题面板" style="width: 100%; margin-top: 20px; margin-left: 30px; padding-bottom: 3px;">';
               $outHtml += '   <div class="nk-receipt-panel b" n="行程段显示" style="width: 300px; text-align: center; margin-top: 20px; margin-left: auto; margin-right: auto; padding-top: 2px; padding-bottom: 2px;">';
               $outHtml += '       <span class="nk-receipt-label lblTravelImage" id="lblTravelImage" n="行程段图片" style="font-size: 18px;"><img src="/content/images/travel.png"></span>';
               $outHtml += '       <span class="nk-receipt-label" id="lblTravelNumber" n="行程段序号" style="width: auto; font-size: 18px;">第'+(index+1)+'段行程：</span>';
               $outHtml += '       <span class="nk-receipt-label" id="lblTravelTag" n="包食宿显示" style="width: auto; font-size: 18px;"> （含食、含宿）</span>';
               $outHtml += '   </div>';
               $outHtml += '     </div>';
               $outHtml += '     <div class="nk-receipt-panel" id="pnlTravelBtns" n="出行标题面板">';
               $outHtml += '   <span class="nk-receipt-label" n="标题" style="width: auto; color: blue; margin-left: 30px; margin-right: 0px;">●</span>';
               $outHtml += '   <span class="nk-receipt-label b" n="本段差旅行程安排" style="width: 796px; color: black; margin-top: 10px; padding-left: 3px;">本段差旅行程安排</span>';
               $outHtml += '   <a href="javascript:void(0);" class="nk-receipt-linkbutton btn-style-del w6 btn btn-danger" id="lnkTravelDelete" n="删除行程" style="float:right;margin: 0px 35px 0px 15px;">删除本段行程</a>';
               $outHtml += '   <a href="javascript:void(0);" class="nk-receipt-linkbutton btn-style-select w6 btn btn-success" id="lnkTravelEdit_'+index+'" n="修改行程" style="float:right;">修改本段行程</a>';
               $outHtml += '     </div>';
               $outHtml += '     <div class="nk-receipt-panel" n="出行人员面板" style="width: 95%; background-color: rgb(236, 245, 255); text-align: center; margin-left: 30px;">';
               $outHtml += '     <span class="nk-receipt-label" n="出行人员" style="margin-right: 40px;">出行人员：';
               $outHtml += '   <span id="lr-info_'+index+'" class="info"><i class="fa fa-info-circle"></i></span>';
               $outHtml += '     </span>';
               $outHtml += '     <span class="nk-receipt-label" id="pnlTravelPerson" default="请选择出行人员" n="出行人员显示区" style="width: 84%;">';

               for (var i = 0, l = route.persons.length; i < l; i++) {
                   $outHtml +=  '<div class="oppbox" style="margin-right:10px;" data-id="'+route.persons[i].id+'"> ';
                   $outHtml +=  '   <span class="oppbox-text black" style="color:#000;">'+route.persons[i].realName+'</span>';
                   $outHtml +=  '</div>';
               }

               $outHtml += '     </span>';
               $outHtml += '     </div>';
               $outHtml += '     <div class="nk-receipt-panel" n="行程显示面板" style="background-color: rgb(255, 255, 255); text-align: center;">';
               $outHtml += '  <span class="nk-receipt-label b" id="pnlTravelCity" n="行程安排显示区" style="width: 920px; padding-left: 10px;">';

               for (var i = 0, l = route.travels.length; i < l; i++) {
                    $outHtml +=  '<div class="oppbox oppbox-border oppbox-city">';
                    $outHtml +=  '   <p><span class="oppbox-text b city-name">'+route.travels[i].cityName+'</span></p>';
                    if(i > 0) {//第一个起点
                       $outHtml +=  '   <p>到达：<span class="oppbox-text time-arrive" style="color:#000;">'+route.travels[i].endDate+'</span></p>';
                    }
                    if(i == 0 || i+1 < l) {//第二个终点
                       $outHtml +=  '   <p>出发：<span class="oppbox-text time-go" style="color:#000;">'+route.travels[i].startDate+'</span></p>';
                    }
                    $outHtml+=  '</div>';
                    if(i+1 < l) {//第二个终点
                       $outHtml +=  '<div class="oppbox oppbox-thougthline">'+route.travels[i].vehicle+'</div>';
                    }
               }

               $outHtml += '</span>';
               $outHtml += '   </div>';
               $outHtml += '   <table class="nk-receipt-list lstTravelInner" cellpadding="0" cellspacing="0" id="lstTravelInner" n="本单位人员行程表格" style="width: 95%; margin-left: 30px;">';
               $outHtml += '   <thead class="nk-receipt-list-thead">';
               $outHtml += '   <tr class="nk-receipt-list-tr" style="text-align: center;">';
               $outHtml += '       <td class="nk-receipt-list-td" n="出行人员">出行人员</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="出发城市时间" style="width: 140px;">出发城市日期</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="到达城市时间" style="width: 140px;">离开或到达城市日期</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="交通工具" style="width: 65px;">交通工具</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="城市间交通费" style="width: 100px;">城市间交通费</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="因私天数" style="width: 70px;">因私天数</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="住宿天数" style="width: 65px;">住宿天数</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="住宿费" style="width: 90px; text-align: center;">住宿费</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="伙食补助费" style="width: 90px;">伙食补助费</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="市内交通费" style="width: 90px;">市内交通费</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="其他费用" style="width: 90px;">其他费用</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="小计" style="width: 100px; text-align: center;">小计</td>';
               $outHtml += '   </tr>';
               $outHtml += '   </thead>';
               $outHtml += '   <tbody class="nk-receipt-list-body">';

               for (var i = 0, l = route.tables.length; i < l; i++) {
                   $outHtml += '   <tr class="nk-receipt-list-tr" style="">';
                   if(i == 0) {
                       $outHtml += '   <td class="nk-receipt-list-td" n="出行人员" rowspan="'+route.tables.length+'" style="text-align: center;">';
                       $outHtml += '       <span class="nk-receipt-label" n="出行人员选择" bind="人员" data-id="'+route.tables[i].personId+'" style="width: auto; text-align: center;">'+route.tables[i].personName+'</span>';
                       $outHtml += '   </td>';
                   }
                   $outHtml += '       <td class="nk-receipt-list-td" n="出发城市时间" style="text-align: center;">';
                   $outHtml += '           <span class="nk-receipt-label" n="出发日期选择" bind="出发日期">'+route.tables[i].startDate+'</span>';
                   $outHtml += '           <br style="clear:both;"><span class="nk-receipt-label" default="选择出发城市" n="出发城市选择" bind="出发城市" style="width: auto;">'+route.tables[i].startCity+'</span>';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="到达城市时间" style="text-align: center;">';
                   $outHtml += '           <span class="nk-receipt-label" n="到达日期选择" bind="离开日期">'+route.tables[i].endDate+'</span><br style="clear:both;">';
                   $outHtml += '           <span class="nk-receipt-label" n="到达城市选择" bind="到达城市" style="width: auto;">'+route.tables[i].endCity+'</span>';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="交通工具" style="text-align: center;">';
                   $outHtml += '           <span class="nk-receipt-label" n="交通工具选择" bind="交通工具">'+route.tables[i].vehicle+'</span>';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="城市间交通费">';
                   $outHtml += '           <input type="text" class="nk-receipt-input margin-5px" n="城市间交通费输入框"'; if(!!route.tables[i].amountCityout) {$outHtml +='value="'+$.formatMoney(route.tables[i].amountCityout, 2)+'"'}  $outHtml += ' style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;">';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="因私天数">';
                   $outHtml += '           <input type="text" class="nk-receipt-input readonly" n="因私天数输入框" readonly="readonly"'; if(!!route.tables[i].daysPrivate) {$outHtml +=' value="'+route.tables[i].daysPrivate+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;">';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="住宿天数">';
                   $outHtml += '           <input type="text" class="nk-receipt-input margin-5px" n="住宿天数输入框"'; if(!!route.tables[i].daysHotel) {$outHtml +=' value="'+route.tables[i].daysHotel+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;">';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="住宿费" style="position: relative; text-align: center;">';
                   $outHtml += '           <input type="text" class="nk-receipt-input margin-5px" n="住宿费输入框"'; if(!!route.tables[i].amountHotel) {$outHtml +=' value="'+$.formatMoney(route.tables[i].amountHotel, 2)+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;">';
                   $outHtml += '           <span class="nk-receipt-label hotelStandard" tag="hotelStandard" n="单双标识" style="width: auto; color: rgb(238, 17, 17); text-align: center; display: none;"></span>';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="伙食补助费">';
                   $outHtml += '           <input type="text" class="nk-receipt-input margin-5px" n="伙食补助费输入框"'; if(!!route.tables[i].amountMeal) {$outHtml +=' value="'+$.formatMoney(route.tables[i].amountMeal, 2)+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;"><span';
                   $outHtml += '               class="nk-receipt-label subsidyDay" tag="subsidyDay3" n="伙食补助天数" style="width: auto; color: rgb(0, 0, 0);"><span>（1天）</span></span></td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="市内交通费">';
                   $outHtml += '           <input type="text" class="nk-receipt-input margin-5px" n="市内交通费输入框"'; if(!!route.tables[i].amountCityinner) {$outHtml +=' value="'+$.formatMoney(route.tables[i].amountCityinner, 2)+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;"><span';
                   $outHtml += '               class="nk-receipt-label subsidyDay" tag="subsidyDay4" n="市内交通费补助天数" style="width: auto; color: rgb(0, 0, 0);"><span>（1天）</span></span>';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td" n="其他费用">';
                   $outHtml += '           <input type="text" class="nk-receipt-input" n="其他费用输入框"'; if(!!route.tables[i].amountOther) {$outHtml +=' value="'+$.formatMoney(route.tables[i].amountOther, 2)+'"'}  $outHtml += '  style="width: 95%; text-align: center; margin-left: 0px; margin-right: 0px;">';
                   $outHtml += '       </td>';
                   $outHtml += '       <td class="nk-receipt-list-td b" n="小计" style="text-align: right; padding-right: 5px;">'; if(!!route.tables[i].amount) {$outHtml +=$.formatMoney(route.tables[i].amount, 2)}  $outHtml += '</td>';
                   $outHtml += '   </tr>';
               }
               $outHtml += '   </tbody>';
               $outHtml += '   <tfoot class="nk-receipt-list-foot" n="表尾">';
               $outHtml += '   <tr class="nk-receipt-list-tr b" n="统计行">';
               $outHtml += '       <td class="nk-receipt-list-td" n="出行人员" style="text-align: right;"></td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="出发城市日期"></td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="到达城市日期"></td>';
               $outHtml += '       <td class="nk-receipt-list-td b" n="交通工具" style="text-align: right;">合计：</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="城市间交通费" style="text-align: center;">0.00</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="因私天数" style="text-align: center;">0</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="住宿天数" style="text-align: center;">0</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="住宿费" style="text-align: center;">0.00</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="伙食补助费" style="text-align: center;">0.00</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="市内交通费" style="text-align: center;">0.00</td>';
               $outHtml += '       <td class="nk-receipt-list-td" n="其他费用" style="text-align: center;">0.00</td>';
               $outHtml += '      <td class="nk-receipt-list-td" n="小计" style="text-align: right; padding-right: 5px;"></td>';
               $outHtml += '   </tr>';
               $outHtml += '   </tfoot>';
               $outHtml += '     </table>';
               $outHtml += ' </div>';

            var $edit = $("#route_div_"+route.billSort)[0];
            if(!!$edit) {
                $edit.outerHTML = $outHtml;
            } else {
                $("#pnlTravel").append($outHtml);
            }
            
            var $tipHtml = "";
              $tipHtml += '<div id="lr-message" class="info-message">';
              $tipHtml += '    <div class="nk-receipt-panel clearfix" n="单间标准面板" style="display: block;">';
              $tipHtml += '        <div class="nk-receipt-panel nk-float-left v-top" n="本次出行人员的相关信息面板">';
              $tipHtml += '            <span class="nk-receipt-label b" n="本次出行人员的相关信息如下：" style="width: 250px; margin-top: 10px; margin-left: 30px;">本次出行人员的相关信息如下：</span>';
              $tipHtml += '            <br style="clear:both;">';
              $tipHtml += '            <table class="nk-receipt-list" cellpadding="0" cellspacing="0" id="lstTravelPerson" n="出行人员表格" style="width: 400px; margin-left: 30px;">';
              $tipHtml += '                <thead class="nk-receipt-list-thead">';
              $tipHtml += '                <tr class="nk-receipt-list-tr">';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="序号" style="width: 40px;">序号</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="姓名" style="width: 90px;">姓名</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="级别" style="width: 80px;">差旅级别</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="人员级别" style="width: 80px;">人员级别</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="所属部门/单位" style="width: 100px;">所属部门/单位</td>';
              $tipHtml += '                </tr>';
              $tipHtml += '                </thead>';
              $tipHtml += '                <tbody class="nk-receipt-list-body">';

              for (var i = 0, l = route.persons.length; i < l; i++) {
                  $tipHtml += '            <tr class="nk-receipt-list-tr" style="">';
                  $tipHtml += '                <td class="nk-receipt-list-td" n="序号" bind="序号" style="text-align: center;">'+route.persons[i].id+'</td>';
                  $tipHtml += '                <td class="nk-receipt-list-td" n="姓名" bind="名称" style="text-align: center;">'+route.persons[i].realName+'</td>';
                  $tipHtml += '                <td class="nk-receipt-list-td" n="级别" bind="差旅级别" style="text-align: center;">其他人员</td>';
                  $tipHtml += '                <td class="nk-receipt-list-td" n="人员级别" bind="级别名称">其他级别人员</td>';
                  $tipHtml += '                <td class="nk-receipt-list-td" n="所属部门/单位" bind="单位" style="text-align: center;">计装处（本处）</td>';
                  $tipHtml += '            </tr>';
              }
              $tipHtml += '                </tbody>';
              $tipHtml += '            </table>';
              $tipHtml += '        </div>';
              $tipHtml += '        <div class="nk-receipt-panel nk-float-left nk-float-clear v-top" n="本次差旅相关标准信息面板" style="width: 490px; margin-left: 35px;">';
              $tipHtml += '            <span class="nk-receipt-label b" n="本次差旅相关标准信息如下：" style="width: 250px; margin-top: 10px;">本次差旅相关标准信息如下：</span>';
              $tipHtml += '            <br style="clear:both;">';
              $tipHtml += '            <table class="nk-receipt-list" cellpadding="0" cellspacing="0" id="lstTravelStandard" n="差旅标准表格" style="width: 490px;">';
              $tipHtml += '                <thead class="nk-receipt-list-thead">';
              $tipHtml += '                <tr class="nk-receipt-list-tr">';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="序号" style="width: 40px;">序号</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="城市" style="width: 130px;">城市</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" tag="省部级" n="省部级住宿" style="width: 82px; display: none;">省部级住宿</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" tag="厅局级" n="厅局级住宿" style="width: 85px; display: none;">厅局级住宿</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" tag="其他人员" n="其他级住宿" style="width: 85px; display: table-cell;">其他级住宿</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="伙食补助费" style="width: 85px;">伙食补助费</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="公杂费" style="width: 85px;">市内交通费</td>';
              $tipHtml += '                    <td class="nk-receipt-list-td" n="城市交通费" style="width: 85px;">城市交通费</td>';
              $tipHtml += '                </tr>';
              $tipHtml += '                </thead>';
              $tipHtml += '                <tbody class="nk-receipt-list-body">';
              for (var i = 0, l = route.travels.length; i < l; i++) {
                  $tipHtml += '                <tr class="nk-receipt-list-tr" style="">';
                  $tipHtml += '                    <td class="nk-receipt-list-td" n="序号" bind="序号" style="text-align: center;">1</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" n="城市" bind="名称" style="text-align: center;">'+route.travels[i].cityName+'</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" tag="省部级" n="省部级" bind="标准1" style="text-align: center; display: none;">900</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" tag="厅局级" n="厅局级" bind="标准2" style="text-align: center; display: none;">550</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" tag="其他人员" n="其他级" bind="标准3" style="text-align: center; display: table-cell;">450</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" n="伙食补助费" bind="标准4" style="text-align: center;">100</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" n="公杂费" bind="标准5" style="text-align: center;">80</td>';
                  $tipHtml += '                    <td class="nk-receipt-list-td" n="城市交通费" bind="包干标准" style="text-align: center;">&nbsp;</td>';
                  $tipHtml += '                </tr>';
              }
              $tipHtml += '                </tbody>';
              $tipHtml += '            </table>';
              $tipHtml += '        </div>';
              $tipHtml += '    </div>';
              $tipHtml += '</div>';

            $("#div_route").append($tipHtml);

            // 编辑行程
            $('#lnkTravelEdit_'+index).on('click', function () {
                var _index = $(this).attr("id").split('_')[1];
                learun.layerForm({
                    id: 'form_route',
                    title: '编辑第'+(index+1)+'段行程',
                    url: config.webportal_server + 'pages/bill3/component/route_form.html?index='+_index+'&access_token=' + config.getToken().access_token,
                    height: 870,
                    width: 1200,
                    callBack: function (id) {
                        return top[id].acceptClick(reloadRouteCallBack);
                    }
                });
            });

            // 标准提示
            $('#lr-info_'+index).hover(
                function () {
                    var obj = $(this).position();
                    $("#lr-message").css("top", obj.top);
                    $("#lr-message").css("left", obj.left+30);
                    $('#lr-message').show();
                },
                function () {
                    $('#lr-message').hide();
                }
            );

            //输入框失去焦点
            $("#lstTravelInner .nk-receipt-input").on("change", function (e) {
                var value = $(this).val();
                var n = $(this).attr("n");
                //自动计算
                autoCalcRoute($(this));

                if(n.indexOf("费")>-1) {
                    $(this).val($.formatMoney(value, 2));
                }
            });
            $("#lstTravelInner").find("input").trigger("change");
        }
    };

    // 设置表单数据
    setFormData = function (processId,param,callback) {
        if (!!processId) {
             $.lrSetForm(config.base_server() + 'billdirects/findByProcessId?processId=' + processId, function (data) {
                 if (data != null) {
                    isUpdate = true;
                    $('#form').lrSetFormData(data);
                    $("#processCode").html(data.processCode);
                    setBillData(data);
                }
             });
        }
        callback && callback();
        $('#lr-layout-panel-right', window.parent.document).show();
    }
    // 验证数据是否填写完整
    validForm = function () {
        if (!$('body').lrValidform()) {
            return false;
        }
        return true;
    };
    // 保存数据
    save = function (processId, callBack, i) {
       var keyValue = "";
       if (isUpdate) {
           keyValue = processId;
       }
       var formData = getBillData();
       var postData = formData;
       postData.id = processId;
       postData.processId = processId;

       $.lrSaveForm(config.base_server() + 'billdirects/saveOrUpdate?keyValue=' + keyValue + '&force=' + 1 + '&access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
           if (res.resp_code != 200) {
               $("#errorMsg").show();
               var html = "<div class=\"alert alert-danger\" role=\"alert\">\n" +
                   "            <p>提示：</p>\n" +
                   "            <p>" + res.resp_msg + "</p>\n" +
                   "       </div>"
               $("#errorMsg").html(html);
           } else {
               $("#errorMsg").hide();
               var html = "<div class=\"alert alert-danger\" role=\"alert\">\n" +
                   "       </div>"
               $("#errorMsg").html(html);

               // 保存成功后才回调
               if (!!callBack) {
                   callBack(res, i);
               }
           }
       });

    };

    //获取表单数据
    function getBillData() {
        var code = $("#code").val();
        var billDate = $("#billDate").val();
        var departmentId = $("#departmentId").lrselectGet();
        var createUsername = $("#createUsername").val();
        var userid = $("#userid").lrformselectGet();
        var telephone = $("#telephone").val();
        var description = $("#description").val();

        var payitemList = [];
        //支出事项主表
        $("#lstPayitemMain").find("tbody").find("tr").each(function (index) {
            var payitem_id = $(this).find("td[name='tdPayitemName']").attr("data-id");
            var payitem_name = $(this).find("td[name='tdPayitemName']").html();
            var payitem_amount = $(this).find("td[n='本次报销金额']").children("input[name='inputPayitemAmount']").val();
            var payitem_description = $(this).find("td[n='报销说明依据']").children("input").val();

            var payitem = {};
            payitem.payitemId = payitem_id;
            payitem.payitemName = payitem_name;
            payitem.amount = $.unformatMoney(payitem_amount);
            payitem.description = payitem_description;

            payitemList.push(payitem);
        });

        //支出事项标准
        $("#lstPayitemForm").find("tbody").find("tr").each(function (index) {
            var $td = $(this).children('td').eq(1);
            var payitem_id = $td.attr("data-id");

            for(var t=0;t<payitemList.length;t++) {
                if(payitemList[t].payitemId==payitem_id) {
                    var payitem = payitemList[t];
                    var $table = $td.children('table');
                    var $tbody = $td.children('table').children('tbody');
                    payitem.forms = [];

                    $tbody.each(function (index1) {
                        var _billIndex = 0;
                        $(this).find("tr").each(function (index2) {
                            var form = {};
                            var _$input = $(this).children('td').eq(1).children("input");
                            form.formValue = _$input.val();
                            form.formId = _$input.attr("data-id");
//                            form.formName = _$input.attr("data-name");
                            if(_$input.attr("data-type")=='number') {
                                form.formValue = $.unformatMoney(_$input.val());
                            } else {
                                form.formValue = _$input.val();
                            }
                            form.billSort = index1;
                            form.billIndex = index2;
                            payitem.forms.push(form);
                        });
                    });
                }
            }
        });

        //行程
        var routeList = [];
        var _routeSort = 0;
        $("#pnlTravel").find("tbody").each(function (index) {
            var _$person;
            var $divPanl = $(this).parent().parent();
            var _includeHotel = $divPanl.attr("data-includeHotel");
            var _includeMeal = $divPanl.attr("data-includeMeal");

            var _routeIndex = 0;
            $(this).find("tr").each(function (index) {
                var travel = {};
                var _index = 1;
                if(index==0) {
                    _index = 0;
                    _$person = $(this).children('td').eq(0).children("span");
                }
                var _$startCityDate = $(this).children('td').eq(1-_index).children("span").eq(0);
                var _$startCityName = $(this).children('td').eq(1-_index).children("span").eq(1);
                var _$endCityDate = $(this).children('td').eq(2-_index).children("span").eq(0);
                var _$endCityName = $(this).children('td').eq(2-_index).children("span").eq(1);
                var _$vehicle = $(this).children('td').eq(3-_index).children("span");
                var _$城市间交通费 = $(this).children('td').eq(4-_index).children("input");
                var _$因私天数 = $(this).children('td').eq(5-_index).children("input");
                var _$住宿天数 = $(this).children('td').eq(6-_index).children("input");
                var _$住宿费 = $(this).children('td').eq(7-_index).children("input");
                var _$伙食补助费 = $(this).children('td').eq(8-_index).children("input");
                var _$室内交通费 = $(this).children('td').eq(9-_index).children("input");
                var _$其他费用 = $(this).children('td').eq(10-_index).children("input");
                var _$小计 = $(this).children('td').eq(11-_index);


                travel.includeHotel = _includeHotel;
                travel.includeMeal = _includeMeal;
                travel.personId = _$person.attr("data-id");
                travel.personName = _$person.html();
                travel.startDate = _$startCityDate.html();
                travel.startCity = _$startCityName.html();
                travel.endDate = _$endCityDate.html();
                travel.endCity = _$endCityName.html();
                travel.vehicle = _$vehicle.html();
                travel.amountCityout = $.unformatMoney(_$城市间交通费.val());
                travel.daysPrivate = _$因私天数.val();
                travel.daysHotel = _$住宿天数.val();
                travel.amountHotel = $.unformatMoney(_$住宿费.val());
                travel.amountMeal = $.unformatMoney(_$伙食补助费.val());
                travel.amountCityinner = $.unformatMoney(_$室内交通费.val());
                travel.amountOther = $.unformatMoney(_$其他费用.val());
                travel.amount = $.unformatMoney(_$小计.html());
                travel.billSort = _routeSort;
                travel.billIndex = _routeIndex;

                routeList.push(travel);
                _routeIndex++;
            });
            _routeSort++;
        });

        //附件
        var attachList = [];
        var _file_sort = 0;
        $("#lstPaperPayment").find("tbody").find("tr").each(function (index) {
            var $td = $(this).children('td').eq(3);

            var $td1 = $(this).children('td').eq(1);
            var $td2 = $(this).children('td').eq(2);

            var _attachId = $td1.attr("data-id");
            var _attachName = $td1.html();
            var _uploadType = $td2.html();
            _file_sort++;
            var attach = {};

            var _folderId = $td.find("div .lrUploader-output").attr("data-id");
            var _description = "";
            var $inputDescription = $td.find("div .lrUploader-output").find("input");
            if(!!$inputDescription) {
                _description = $inputDescription.val();
            }

            var attach = {};
            attach.attachId = _attachId;
            attach.attachName = _attachName;
            attach.attachUploadType = _uploadType;
            attach.folderId = _folderId;
            attach.fileSort = _file_sort;
            attach.description = _description;
            attachList.push(attach);
        });

        //结算方式
        var settlementList = [];
        $("#lstSettlement").children("tbody").each(function (index) {
            var $lstTrSettlement1Tr = $(this).find("tr[id='lstTrSettlement1']");
            var $lstTrSettlement2Tr = $(this).find("tr[id='lstTrSettlement2']");
            var $lstTrSettlement3Tr = $(this).find("tr[id='lstTrSettlement3']");
            var $lstTrSettlement4Tr = $(this).find("tr[id='lstTrSettlement4']");

            var settlement1 = {
                category: $lstTrSettlement1Tr.find("td").eq(0).html(),
                amount: $.unformatMoney($lstTrSettlement1Tr.find("td").eq(1).children("input").val()),
                description: $lstTrSettlement1Tr.find("td").eq(2).find("input").val(),
                payeeList: []
            }
            var settlement2 = {
                category: $lstTrSettlement2Tr.find("td").eq(0).html(),
                amount: $.unformatMoney($lstTrSettlement2Tr.find("td").eq(1).children("input").val()),
                description: $lstTrSettlement2Tr.find("td").eq(2).find("input").val()
            }
            var settlement3 = {
                category: $lstTrSettlement3Tr.find("td").eq(0).html(),
                amount: $.unformatMoney($lstTrSettlement3Tr.find("td").eq(1).children("input").val()),
                description: $lstTrSettlement3Tr.find("td").eq(2).find("input").val(),
                payeeList: []
            }
            var settlement4 = {
                category: $lstTrSettlement4Tr.find("td").eq(0).html(),
                amount: $.unformatMoney($lstTrSettlement4Tr.find("td").eq(1).children("input").val()),
                description: $lstTrSettlement4Tr.find("td").eq(2).find("input").val(),
                payeeList: []
            };

            if($("#_settlement1").is(':checked')) {
                settlementList.push(settlement1);
                $("#lstCheque").find("tbody").find("tr").each(function (index) {
                    var payeeName = $(this).find("td").eq(0).html();
                    var payeeBankName = $(this).find("td").eq(1).children("input").val();
                    var payeeBankAccount = $(this).find("td").eq(2).children("input").val();
                    var payeeAmount = $(this).find("td").eq(3).children("input").val();

                    var payeeItem = {};
                    payeeItem.payeeName = payeeName;
                    payeeItem.payeeBankName = payeeBankName;
                    payeeItem.payeeBankAccount = payeeBankAccount;
                    payeeItem.payeeAmount = $.unformatMoney(payeeAmount);

                    settlement1.payeeList.push(payeeItem);
                });
            }

            if($("#_settlement2").is(':checked')) {
                settlementList.push(settlement2);

            }
            if($("#_settlement3").is(':checked')) {
                settlementList.push(settlement3);
                $("#lstCivilServiceCard").find("tbody").find("tr").each(function (index) {
                    var payeeName = $(this).find("td").eq(0).html();
                    var payeeBankAccount = $(this).find("td").eq(1).html();
                    var payeeDate = $(this).find("td").eq(2).children("input").val();
                    var payeeAmount = $(this).find("td").eq(3).children("input").val();

                    var payeeItem = {};
                    payeeItem.payeeName = payeeName;
                    payeeItem.payeeBankAccount = payeeBankAccount;
                    payeeItem.payeeDate = payeeDate;
                    payeeItem.payeeAmount = $.unformatMoney(payeeAmount);
                    settlement3.payeeList.push(payeeItem);
                });
            }
            if($("#_settlement4").is(':checked')) {
                settlementList.push(settlement4);
                $("#lstTransfer").find("tbody").find("tr").each(function (index) {
                    var payeeName = $(this).find("td").eq(0).html();
                    var payeeBankName = $(this).find("td").eq(1).children("input").val();
                    var payeeBankAccount = $(this).find("td").eq(2).children("input").val();
                    var payeeAmount = $(this).find("td").eq(3).children("input").val();

                    var payeeItem = {};
                    payeeItem.payeeName = payeeName;
                    payeeItem.payeeBankName = payeeBankName;
                    payeeItem.payeeBankAccount = payeeBankAccount;
                    payeeItem.payeeAmount = $.unformatMoney(payeeAmount);
                    settlement4.payeeList.push(payeeItem);
                });
            }
            settlementList.push(settlement1);
            settlementList.push(settlement2);
            settlementList.push(settlement3);
            settlementList.push(settlement4);
        });

        billData.code = code;
        billData.billDate = billDate;
        billData.departmentId = departmentId;
        billData.createUsername = createUsername;
        billData.userid = userid;
        billData.telephone = telephone;
        billData.description = description;
        billData.payitemList = payitemList;
        billData.routeList = routeList;
        billData.attachList = attachList;
        billData.settlementList = settlementList;

        return billData;
    }

    //设置表单数据
    function setBillData(billData) {
        $("#code").val(billData.code);
        $("#billDate").val(billData.billDate);
        $("#departmentId").lrselectSet(billData.departmentId);
        $("#createUsername").val(billData.createUsername);
        $("#userid").lrformselectSet(billData.userid);
        $("#telephone").val(billData.telephone);
        $("#description").val(billData.description);

        //支出事项主表
        for(var i=0;i<billData.payitemList.length;i++) {
            billData.payitemList[i].id = billData.payitemList[i].payitemId;
            billData.payitemList[i].name = billData.payitemList[i].payitemName;
        }
        reloadPayitemCallBack(billData.payitemList, 1, billData.attachList);

        //行程
        var _persons = [];
        var _travels = [];
        var _tables = [];
        var _currentSort = 0;
        for(var i=0;i<billData.routeList.length;i++) {
            var _route = billData.routeList[i];

            var _sort = _route.billSort;
            var _personNames = _route.personName.split(',');
            var _personIds = _route.personId.split(',');

            if(_currentSort != _sort) {
                _travels = [];
                _persons = [];
                _tables = [];
                _currentSort++;
            }
            for(var p=0;p<_personNames.length;p++) {
                var _exists = false;
                for(var c=0;c<_persons.length;c++) {
                    if(_persons[c].id==_personIds[p]) {
                        _exists = true;
                        break;
                    }
                }
                if(!_exists) {
                    _persons.push({id:_personIds[p],realName:_personNames[p]});
                }
            }

            if(routes.length<_sort+1) {
                routes.push({});
            }
            routes[_sort].persons = _persons;

            if(_route.billIndex == 0){
                var travel = {};
                travel.id = _route.id;
                travel.cityName = _route.startCity;
                travel.startDate = _route.startDate;
                travel.endDate = _route.endDate;
                travel.vehicle = _route.vehicle;
                _travels.push(travel);
            }
            var travel = {};
            travel.id = "id";
            travel.cityName = _route.endCity;
            travel.startDate = _route.startDate;
            travel.endDate = _route.endDate;
            travel.vehicle = _route.vehicle;
            _travels.push(travel);
            routes[_sort].travels = _travels;

            _route.persons = _route.personName;
            _tables.push(_route);
            routes[_sort].tables = _tables;

            routes[_sort].includeMeal = _route.includeMeal;
            routes[_sort].includeHotel = _route.includeHotel;
            routes[_sort].billSort = _route.billSort;
        }

        for(var p=0;p<routes.length;p++) {
            page.redrawRoute(routes[p], p);
        }

        //结算方式
        for(var s=0;s<billData.settlementList.length;s++){
            var settlement = billData.settlementList[s];
            if(settlement.category=='现金/支票') {
                var _payeeList = [];
                for(var p=0;p<settlement.payeeList.length;p++) {
                    var payee = {};
                    payee.name = settlement.payeeList[p].payeeName;
                    payee.bankName = settlement.payeeList[p].payeeBankName;
                    payee.bankAccount = settlement.payeeList[p].payeeBankAccount;
                    payee.amount = settlement.payeeList[p].payeeAmount;
                    _payeeList.push(payee);
                }
                reloadCashSupplierCallBack(_payeeList);
                $("#_settlement1").prop('checked', true);
                $("#_settlement1").trigger("change");
                $("#lstTrSettlement1").children("td").eq(1).children("input").val($.formatMoney(settlement.amount, 2));
                $("#lstTrSettlement1").children("td").eq(2).find("input").val(settlement.description);
            } else if(settlement.category=='市财政局直接支付') {
                $("#_settlement2").prop('checked', true);
                $("#_settlement2").trigger("change");
                $("#lstTrSettlement2").children("td").eq(1).children("input").val($.formatMoney(settlement.amount, 2));
                $("#lstTrSettlement2").children("td").eq(2).find("input").val(settlement.description);
            } else if(settlement.category=='公务卡') {
                var _payeeList = [];
                for(var p=0;p<settlement.payeeList.length;p++) {
                    var payee = {};
                    payee.realName = settlement.payeeList[p].payeeName;
                    payee.bankName = settlement.payeeList[p].payeeBankName;
                    payee.officialBankAccount = settlement.payeeList[p].payeeBankAccount;
                    payee.amount = settlement.payeeList[p].payeeAmount;
                    _payeeList.push(payee);
                }
                reloadCivilServiceCardCallBack(_payeeList);
                $("#_settlement3").prop('checked', true);
                $("#_settlement3").trigger("change");
                $("#lstTrSettlement3").children("td").eq(1).children("input").val($.formatMoney(settlement.amount, 2));
                $("#lstTrSettlement3").children("td").eq(2).find("input").val(settlement.description);
            } else if(settlement.category=='转账') {
                var _payeeList = [];
                for(var p=0;p<settlement.payeeList.length;p++) {
                    var payee = {};
                    payee.name = settlement.payeeList[p].payeeName;
                    payee.bankName = settlement.payeeList[p].payeeBankName;
                    payee.bankAccount = settlement.payeeList[p].payeeBankAccount;
                    payee.amount = settlement.payeeList[p].payeeAmount;
                    _payeeList.push(payee);
                }
                reloadTransferSupplierCallBack(_payeeList);
                $("#_settlement4").prop('checked', true);
                $("#_settlement4").trigger("change");
                $("#lstTrSettlement4").children("td").eq(1).children("input").val($.formatMoney(settlement.amount, 2));
                $("#lstTrSettlement4").children("td").eq(2).find("input").val(settlement.description);
            }
        }
        autoCalcSettlement();
    }

    //加载支出事项标准面板
    function loadPayitemForm(data) {
       var $trHtml = "";

       var $tbody = $("#lstPayitemForm").find("tbody");

       $.each(data, function(key, value) {
          var _payitem = key.split(',');
          if(value.length>0) {
              if ($("#divPayitemForm").is(':hidden')) {
                  $("#divPayitemForm").show();
              }
              $trHtml += '<tr>';
              $trHtml += '    <td class="b need-btn" style="position:relative;">〖'+_payitem[1]+'〗</td>';
              $trHtml += '    <td colspan="2" class="l" data-id="'+_payitem[0]+'">';
              if(value.length>0) {
                 var _map = {};
                 var _forms = [];
                 var _billSort = 0;
                 for(var f=0;f<value.length;f++) {
                     if(value[f].billSort==_billSort) {
                        _forms.push(value[f]);
                     } else {
                        _forms = [];
                        _billSort++;
                        _forms.push(value[f]);
                     }
                     _map[value[f].billSort] = _forms;
                 }

                  $.each(_map, function(key2, value2) {
                      var _defaultOption;
                      for(var f=0;f<value2.length;f++) {
                          if(value2[f].selectList.length>0) {
                             for(var x=0;x<value2[f].selectList.length;x++) {
                                if(value2[f].selectList[x].confValue==value2[f].value) {
                                    _defaultOption = value2[f].selectList[x];
                                    break;
                                }
                             }
                             if(!_defaultOption) {
                                 for(var x=0;x<value2[f].selectList.length;x++) {
                                    if(value2[f].selectList[x].isDefault=='1') {
                                        _defaultOption = value2[f].selectList[x];
                                        break;
                                    }
                                 }
                             }
                          }
                      }
                      if(_defaultOption) {
                          $trHtml += '<table border="0" data-id="'+_defaultOption.payitemFormId+'" cellpadding="0" cellspacing="0" style="width:550px; float:left; margin-right:5px;" class="nk-receipt-list">';
                          $trHtml += '  <thead class="nk-receipt-list-thead">';
                          $trHtml += '    <tr>';
                          $trHtml += '   <td style="width:200px;"></td>';
                          $trHtml += '   <td style="text-align:left;width:200px;"><a class="b" style="cursor:pointer;" name="btnSelectedPayitemFormOption" data-id="'+_defaultOption.id+'">'+_defaultOption.confName+'</a></td>';
                          $trHtml += '   <td></td>';
                      } else {
                          $trHtml += '<table border="0" cellpadding="0" cellspacing="0" style="width:550px; float:left; margin-right:5px;" class="nk-receipt-list">';
                          $trHtml += '  <thead class="nk-receipt-list-thead">';
                          $trHtml += '    <tr>';
                          $trHtml += '   <td style="width:200px;background: none;border: none;"></td>';
                          $trHtml += '   <td style="text-align:left;width:200px;background: none;border: none;"><a class="b"></a></td>';
                          $trHtml += '   <td style="background: none;border: none;"></td>';
                      }
                      $trHtml += '     </tr>';
                      $trHtml += '  </thead>';
                      $trHtml += '<tbody>';
                      for(var f=0;f<value2.length;f++) {
                         var _required = "";
                         var _readonly = "";
                         if(value2[f].required=='1') {
                             _required = '<span class="red">*</span>';
                         }
                         if(value2[f].readonly=='1') {
                             _readonly = 'readonly="readonly"';
                         }
                         $trHtml += '<tr>';
                         $trHtml += '  <td>'+_required+''+value2[f].name+'：</td>';
                         if(_defaultOption.payitemFormId == value2[f].id) {
                            if(!value2[f].value) {
                                value2[f].value = _defaultOption.confValue;
                            }
                         }
                         $trHtml += '  <td><input type="text" '+_readonly+' class="blue lineinput" data-id="'+value2[f].id+'" data-name="'+value2[f].name+'" data-type="'+value2[f].dataType+'" data-formula="'+value2[f].bgtFormula+'" '; if(!!value2[f].value){ $trHtml +=' value="'+$.formatMoney(value2[f].value, 2)+'"'} $trHtml += ' style="background-color:#fff;"/></td>';
                         $trHtml += '  <td></td>';
                         $trHtml += '</tr>';
                      }
                      $trHtml += '   </tbody>';
                      $trHtml += '</table>';
                  });
              }
              $trHtml += '  </td>';
              $trHtml += '</tr>';
          }
       });

       $tbody.empty();
       $tbody.append($trHtml);

       // 选择选项
       $("a[name=btnSelectedPayitemFormOption").on('click', function () {
           var formId = $(this).parent().parent().parent().parent().attr("data-id");
           _currentPayitemFromObj = $(this);
           learun.layerForm({
               id: 'form_payitem_form_select',
               title: '选择标准',
               url: config.webportal_server + 'pages/base/payitem/form/multi_select.html?formId='+formId+'&access_token=' + config.getToken().access_token,
               height: 570,
               width: 600,
               callBack: function (id) {
                   return top[id].acceptClick(reloadPayitemFormOptionCallBack);
               }
           });
       });

       //输入框失去焦点
       $("#lstPayitemForm input").on("change", function (e) {
           var value = $(this).val();
           var dataType = $(this).attr("data-type");

           var $tbody = $(this).parent().parent().parent();
           $tbody.find("input").each(function (index) {
               var formula = $(this).attr("data-formula");
               if(!!formula) {
                   $(this).val($.formatMoney(eval(formula), 2));
               }
           });
           if(dataType=='number') {
               $(this).val($.formatMoney(value, 2));
           }
       });
    }

    //文件预览
    function previewFile(url) {
        learun.layerForm({
            id: 'PreviewUrl',
            title: '文件预览',
            url: config.webportal_server + 'pages/utility/previewUrl.html?url=' + url + '&access_token=' + config.getToken().access_token,
            width: 700,
            height: 850,
            btn: null
        });
    }

    //自动计算金额---行程
    function autoCalcRoute(obj) {
        var value = $(obj).val();
        var n = $(obj).attr("n");
        var $tr = $(obj).parent().parent();
        var $tbody = $(obj).parent().parent().parent();
        var $trAll = $tbody.find("tr");
        var $tfoot = $(obj).parent().parent().parent().parent().children("tfoot");

        var t_城市间交通费 = 0;
        var t_住宿天数 = 0;
        var t_住宿费 = 0;
        var t_伙食补助费 = 0;
        var t_市内交通费 = 0;
        var t_其他费用 = 0;
        var t_小计 = 0;
        for(var i=0;i<$trAll.length;i++) {
            var v_城市间交通费 = $.unformatMoney($($trAll[i]).find("td[n='城市间交通费']").children("input").val());
            var v_住宿天数 = $($trAll[i]).find("td[n='住宿天数']").children("input").val();
            var v_住宿费 = $.unformatMoney($($trAll[i]).find("td[n='住宿费']").children("input").val());
            var v_伙食补助费 = $.unformatMoney($($trAll[i]).find("td[n='伙食补助费']").children("input").val());
            var v_市内交通费 = $.unformatMoney($($trAll[i]).find("td[n='市内交通费']").children("input").val());
            var v_其他费用 = $.unformatMoney($($trAll[i]).find("td[n='其他费用']").children("input").val());
            var v_小计 = $.unformatMoney($($trAll[i]).find("td[n='小计']").html());

            v_住宿费 = Number(v_住宿天数) * 450;
            var $current_tr住宿费 = $($trAll[i]).find("td[n='住宿费']").children("input");
            $current_tr住宿费.val($.formatMoney(v_住宿费, 2));

            v_小计 = calc.add3(Number(v_城市间交通费), Number(v_住宿费), Number(v_伙食补助费), Number(v_市内交通费), Number(v_其他费用));
            var $current_tr小计 = $($trAll[i]).find("td[n='小计']");
            $current_tr小计.html($.formatMoney(v_小计, 2));

            t_城市间交通费 = calc.add(t_城市间交通费, Number(v_城市间交通费));
            t_住宿天数 = calc.add(t_住宿天数, Number(v_住宿天数));
            t_住宿费 = calc.add(t_住宿费, Number(v_住宿费));
            t_伙食补助费 = calc.add(t_伙食补助费, Number(v_伙食补助费));
            t_市内交通费 = calc.add(t_市内交通费, Number(v_市内交通费));
            t_其他费用 = calc.add(t_其他费用, Number(v_其他费用));
            t_小计 = calc.add(t_小计, Number(v_小计));
        }
        var $foot城市间交通费 = $($tfoot).find("td[n='城市间交通费']");
        $foot城市间交通费.html($.formatMoney(t_城市间交通费, 2));

        var $foot住宿天数 = $($tfoot).find("td[n='住宿天数']");
        $foot住宿天数.html(t_住宿天数);

        var $foot住宿费 = $($tfoot).find("td[n='住宿费']");
        $foot住宿费.html($.formatMoney(t_住宿费, 2));

        var $foot伙食补助费 = $($tfoot).find("td[n='伙食补助费']");
        $foot伙食补助费.html($.formatMoney(t_伙食补助费, 2));

        var $foot市内交通费 = $($tfoot).find("td[n='市内交通费']");
        $foot市内交通费.html($.formatMoney(t_市内交通费, 2));

        var $foot其他费用 = $($tfoot).find("td[n='其他费用']");
        $foot其他费用.html($.formatMoney(t_其他费用, 2));

        var $foot小计 = $($tfoot).find("td[n='小计']");
        $foot小计.html($.formatMoney(t_小计, 2));
    }

    //自动计算金额---支出事项
    function autoCalcPayitem(obj) {
        var value = $(obj).val();
        var $tr = $(obj).parent().parent();
        var $tbody = $(obj).parent().parent().parent();
        var $trAll = $tbody.find("tr");
        var $tfoot = $(obj).parent().parent().parent().parent().children("tfoot");

        var t_amount = 0;
        for(var i=0;i<$trAll.length;i++) {
            var v_amount = $.unformatMoney($($trAll[i]).find("td[n='本次报销金额']").children("input[name='inputPayitemAmount']").val());

            t_amount = calc.add(t_amount, Number(v_amount));
        }
        var $footAmount = $($tfoot).find("td[n='本次申请合计金额']").children("span[n='显示合计金额']");
        $footAmount.html($.formatMoney(t_amount, 2));
    }

    //自动计算金额---结算方式
    function autoCalcSettlement() {
        var $tbody = $("#lstSettlement").children("tbody");
        var $trAll = $tbody.find("tr");
        var $tfoot = $("#lstSettlement").children("tfoot");

        var t_amount = 0;
        for(var i=0;i<$trAll.length;i++) {
            var v_amount = $.unformatMoney($($trAll[i]).find("td").eq(1).children("input").val());
            t_amount = calc.add(t_amount, Number(v_amount));
        }
        var $footAmount = $($tfoot).find("td[id='lblSettlementTotal']");
        $footAmount.html($.formatMoney(t_amount, 2));
    }

    //设置行程回调
    reloadPayitemCallBack = function(items, flag, attachList) {
        var $tbody = $("#lstPayitemMain").find("tbody");
        var $trHtml = "";

        var _payitemIds = [];
        for(var i=0;i<items.length;i++) {
            var item = items[i];
            _payitemIds.push(item.id);

            if(item.name=="差旅费") {
               if ($("#div_route").is(':hidden')) {
                  $("#div_route").show();
               }
            }

            $trHtml += '<tr class="nk-receipt-list-tr" style="">';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">一般公共预算</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">公用经费</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;" name="tdPayitemName" data-id="'+item.id+'">'+item.name+'</td>';
            $trHtml += '    <td class="nk-receipt-list-td" n="本次报销金额">';
            $trHtml += '        <input type="text" maxlength="16" class="nk-receipt-input blue" name="inputPayitemAmount" '; if(!!item.amount) {$trHtml +='value="'+$.formatMoney(item.amount, 2)+'"'}  $trHtml += ' style="width: 85%; text-align: right; margin-right: 0px; padding-right: 2px;">';
            $trHtml += '        <span class="nk-receipt-label" style="width: auto; margin-left: 0px;">元</span>';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" n="报销说明依据">';
            $trHtml += '        <span class="nk-receipt-label" style="width: auto; margin-left: 50px;">说明依据：</span>';
            $trHtml += '        <input type="text" maxlength="512" class="nk-receipt-input blue" name="inputPayitemDescription" value="'+item.description+'" style="width: 80%; text-align: left; ">';
            $trHtml += '    </td>';
            $trHtml += '</tr>';
        }
        payitemIds = _payitemIds.join(",");
        $tbody.empty();
        $tbody.append($trHtml);

        //输入框失去焦点
        $("#lstPayitemMain input[name='inputPayitemAmount']").on("change", function (e) {
            var value = $(this).val();
            var name = $(this).attr("name");
            //自动计算
            autoCalcPayitem($(this));

            if(name.indexOf("Amount")>-1) {
                $(this).val($.formatMoney(value, 2));
            }
        });

        if(flag==1) { //加载数据
            var _map = {};
            for(var i=0;i<items.length;i++) {
                var item = items[i];
                var _forms = [];
                if(!!item.forms) {
                    for(var j=0;j<item.forms.length;j++) {
                        var _formdata = item.forms[j].form;
                        _formdata.value = item.forms[j].formValue;
                        _formdata.billSort = item.forms[j].billSort;
                        _formdata.billIndex = item.forms[j].billIndex;
                        _forms.push(item.forms[j].form);
                    }
                }
                _map[item.payitemId+","+item.payitemName] = _forms;
            }
            loadPayitemForm(_map);
        } else { //新建单据
            //获取测算表单
            learun.httpAsync('GET', config.base_server() + 'basepayitemforms/findByPayitemIds?access_token=' + config.getToken().access_token, { payitemIds: _payitemIds.join(",") }, function (data) {
                if (data) {
                   loadPayitemForm(data);
                }
             });
        }

        //获取附件
        learun.httpAsync('GET', config.base_server() + 'basepayitemattachs/findByPayitemIds?access_token=' + config.getToken().access_token, { payitemIds: _payitemIds.join(","),category: "reimburse" }, function (data) {
            if (data) {
               var $tbody = $("#lstPaperPayment").find("tbody");
               for(var i=0;i<data.length;i++) {
                  var $trHtml = "";
                  $trHtml += '<tr class="nk-receipt-list-tr">';
                  $trHtml += '    <td class="nk-receipt-list-td" n="序号" style="text-align: center;">'+(i+1)+'</td>';
                  $trHtml += '    <td class="nk-receipt-list-td" n="清单名称" data-id="'+data[i].id+'" style="text-align: left;">'+data[i].name+'</td>';
                  $trHtml += '    <td class="nk-receipt-list-td" n="提交要求" style="text-align: center;">'+data[i].uploadType+'</td>';
                  $trHtml += '    <td class="nk-receipt-list-td" n="上传链接" style="text-align: center;">';
                  $trHtml += '       <div id="'+data[i].id+'" isvalid="yes" checkexpession="NotNull"></div>';
                  $trHtml += '    </td>';
                  $trHtml += '</tr>';
                  $tbody.append($trHtml);

                  $("#"+data[i].id).lrUploader({isView: false, processId: '', isInvoice: 0});

                  if(!!attachList) {
                     for(var f=0;f<attachList.length;f++) {
                         if(attachList[f].attachId == data[i].id) {
                            if(!!attachList[f].folderId && attachList[f].folderId>"") {
                                $("#"+data[i].id).lrUploaderSet(attachList[f].folderId);
                            } else if(!!attachList[f].description) {
                                $("#"+data[i].id+" input").val(attachList[f].description);
                            }
                         }
                     }
                  }
               }
            }
        });
    }

    //选择支出事项标准选项回调
    reloadPayitemFormOptionCallBack = function(options) {
        var $table = $(_currentPayitemFromObj).parent().parent().parent().parent();
        var $tableParentTd = $(_currentPayitemFromObj).parent().parent().parent().parent().parent();
        var payitemId = $($tableParentTd).attr("data-id");

        if(options.length==1) {
            var option = options[0];
            $(_currentPayitemFromObj).attr("data-id",option.id);
            $(_currentPayitemFromObj).html(option.confName);
            $($tableParentTd).children().not(":first").remove(); //删除非第一个table
        } else {
            learun.httpAsync('GET', config.base_server() + 'basepayitemforms/findByPayitemId?access_token=' + config.getToken().access_token, { payitemId: payitemId }, function (data) {
               if (data) {
                  var $trHtml = "";
                  for(var i=0;i<options.length;i++) {
                     var option = options[i];
                     var currentOptionId = $(_currentPayitemFromObj).attr("data-id");

                     $trHtml += '<table data-id="'+option.payitemFormId+'" border="0" cellpadding="0" cellspacing="0" style="width:550px; float:left; margin-right:5px;" class="nk-receipt-list">';
                     $trHtml += '  <thead class="nk-receipt-list-thead">';
                     $trHtml += '    <tr>';
                     $trHtml += '      <td style="width:200px;"></td>';
                     $trHtml += '      <td style="text-align:left;width:200px;"><a class="b" style="cursor:pointer;" name="btnSelectedPayitemFormOption" data-id="'+option.id+'">'+option.confName+'</a></td>';
                     $trHtml += '      <td></td>';
                     $trHtml += '    </tr>';
                     $trHtml += '  </thead>';
                     $trHtml += '<tbody>';

                     for(var f=0;f<data.length;f++) {
                        var _required = "";
                        var _readonly = "";
                        if(data[f].required=='1') {
                            _required = '<span class="red">*</span>';
                        }
                        if(data[f].readonly=='1') {
                            _readonly = 'readonly="readonly"';
                        }
                        $trHtml += '<tr>';
                        $trHtml += '  <td>'+_required+''+data[f].name+'：</td>';
                        if(option.payitemFormId == data[f].id) {
                            data[f].value = option.confValue;
                        }
                        $trHtml += '  <td><input type="text" '+_readonly+' class="blue lineinput" data-id="'+data[f].id+'" data-name="'+data[f].name+'" data-type="'+data[f].dataType+'" data-formula="'+data[f].bgtFormula+'" '; if(!!data[f].value){ $trHtml +=' value="'+$.formatMoney(data[f].value, 2)+'"'} $trHtml += ' style="background-color:#fff;"/></td>';
                        $trHtml += '  <td></td>';
                        $trHtml += '</tr>';
                     }
                     $trHtml += '   </tbody>';
                     $trHtml += '</table>';
                  }
                  $tableParentTd.empty();
                  $tableParentTd.append($trHtml);

                  // 选择选项
                  $("a[name=btnSelectedPayitemFormOption").on('click', function () {
                     var formId = $(this).parent().parent().parent().parent().attr("data-id");
                     _currentPayitemFromObj = $(this);
                     learun.layerForm({
                        id: 'form_payitem_form_select',
                        title: '选择标准',
                        url: config.webportal_server + 'pages/base/payitem/form/multi_select.html?formId='+formId+'&access_token=' + config.getToken().access_token,
                        height: 570,
                        width: 600,
                        callBack: function (id) {
                            return top[id].acceptClick(reloadPayitemFormOptionCallBack);
                        }
                     });
                  });

                   //输入框失去焦点
                  $("#lstPayitemForm input").on("change", function (e) {
                     var value = $(this).val();
                     var dataType = $(this).attr("data-type");

                     var $tbody = $(this).parent().parent().parent();
                     $tbody.find("input").each(function (index) {
                         var formula = $(this).attr("data-formula");
                         if(!!formula) {
                             $(this).val($.formatMoney(eval(formula), 2));
                         }
                     });
                     if(dataType=='number') {
                         $(this).val($.formatMoney(value, 2));
                     }
                 });
               }
            });
        }
    }

    //设置行程回调
    reloadRouteCallBack = function(route, index) {
        var tables = [];
        for(var i=0;i<route.travels.length;i++) {
            if(i>0){
               var row = {};
               row.id=i-1;
               var personNameStr = "";
               var personIdStr = "";
               for(var p=0;p<route.persons.length;p++) {
                  if(personNameStr=="") {
                     personIdStr += route.persons[p].id;
                     personNameStr += route.persons[p].realName;
                  } else {
                     personIdStr += route.persons[p].id;
                     personNameStr += "、" + route.persons[p].realName;
                  }
               }
               row.personId=personIdStr;
               row.personName=personNameStr;
               row.startCity=route.travels[i-1].cityName;
               row.endCity=route.travels[i].cityName;
               row.startDate=route.travels[i-1].startDate;
               row.endDate=route.travels[i].endDate;
               row.vehicle=route.travels[i].vehicle;
               tables.push(row);
            }
        }
        route.tables = tables;
        if(!!index) {
            routes[index] = route;
        } else {
            index = routes.length;
            route.billSort = index;
            routes.push(route);
        }
        page.redrawRoute(route, Number(index));
    }

    //设置现金/支票收款人回调
    reloadCashSupplierCallBack = function(suppliers, index) {
        var $tbody = $("#lstCheque").find("tbody");
        var $trHtml = "";

        for(var i=0;i<suppliers.length;i++) {
            var item = suppliers[i];
            $trHtml += '<tr class="nk-receipt-list-tr">';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">'+item.name+'</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="position: relative;">';
            $trHtml += '        <input type="text" class="nk-receipt-input" maxlength="100" value="'+item.bankName+'" style="width: 95%; padding-left: 0px; padding-right: 5px; box-sizing: content-box;">';
            $trHtml += '        <p class="maxlength" style="position: absolute; right: 11px; top: 4.94446px; display: none;">';
            $trHtml += '            <span style="color:#11cc43;">13</span>/<span style="color:red"> 100</span>';
            $trHtml += '        </p>';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td">';
            $trHtml += '        <input type="text" class="nk-receipt-input" maxlength="30" value="'+item.bankAccount+'" style="width: 95%; text-align: center; padding-left: 5px; padding-right: 0px;">';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <input type="text" class="nk-receipt-input" ';if(!!item.amount){ $trHtml +=' value="'+$.formatMoney(item.amount, 2)+'"'}  $trHtml += '  style="width: 80%; text-align: right; margin-right: 0px;">';
            $trHtml += '        <span class="nk-receipt-label" style="width: 20px;">元</span>';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <a href="javascript:void(0);" class="nk-receipt-linkbutton p-del btn-style-del w2" name="btnDeleteCashSupplier">删除</a>';
            $trHtml += '    </td>';
            $trHtml += '</tr>';
        }
        $tbody.empty();
        $tbody.append($trHtml);

        //删除行记录
        $("#lstCheque a[name='btnDeleteCashSupplier']").on("click", function (e) {
            var $btnDelete = this;
            learun.layerConfirm('是否确认删除该项?', function (res, index) {
                if (res) {
                    $($btnDelete).parent().parent().remove();
                    top.layer.close(index); //再执行关闭
                }
            });
        });

        //输入框失去焦点
        $("#lstCheque tbody").find("tr").each(function(index) {
            var $trSettlement1Tr = $("#lstSettlement").children("tbody").find("tr[id='lstTrSettlement1']");
            var $trSettlement1Amount = $trSettlement1Tr.children("td").eq(1).find("input");
            $(this).find("td").eq(3).find("input").on("change", function (e) {
               var value = $(this).val();
               $(this).val($.formatMoney(value, 2));

               var _total = 0;
               $("#lstCheque tbody").find("tr").each(function(index) {
                   $(this).find("td").eq(3).find("input").each(function(index) {
                      var _value = $(this).val();
                      _total = calc.add(_total, Number(_value));
                   });
               });
               $trSettlement1Amount.val($.formatMoney(_total, 2));
               autoCalcSettlement();
            });
        })
    }

    //设置转账收款人回调
    reloadTransferSupplierCallBack = function(suppliers, index) {
        var $tbody = $("#lstTransfer").find("tbody");
        var $trHtml = "";

        for(var i=0;i<suppliers.length;i++) {
            var item = suppliers[i];
            $trHtml += '<tr class="nk-receipt-list-tr">';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">'+item.name+'</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="position: relative;">';
            $trHtml += '        <input type="text" class="nk-receipt-input" maxlength="100" value="'+item.bankName+'" style="width: 95%; padding-left: 0px; padding-right: 5px; box-sizing: content-box;">';
            $trHtml += '        <p class="maxlength" style="position: absolute; right: 11px; top: 4.94446px; display: none;">';
            $trHtml += '            <span style="color:#11cc43;">13</span>/<span style="color:red"> 100</span>';
            $trHtml += '        </p>';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td">';
            $trHtml += '        <input type="text" class="nk-receipt-input" maxlength="30" value="'+item.bankAccount+'" style="width: 95%; text-align: center; padding-left: 5px; padding-right: 0px;">';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <input type="text" class="nk-receipt-input" ';if(!!item.amount){ $trHtml +=' value="'+$.formatMoney(item.amount, 2)+'"'}  $trHtml += '    style="width: 80%; text-align: right; margin-right: 0px;">';
            $trHtml += '        <span class="nk-receipt-label" style="width: 20px;">元</span>';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <a href="javascript:void(0);" class="nk-receipt-linkbutton p-del btn-style-del w2" name="btnDeleteTransferSupplier">删除</a>';
            $trHtml += '    </td>';
            $trHtml += '</tr>';
        }
        $tbody.empty();
        $tbody.append($trHtml);

        //删除行记录
        $("#lstTransfer a[name='btnDeleteTransferSupplier']").on("click", function (e) {
            var $btnDelete = this;
            learun.layerConfirm('是否确认删除该项?', function (res, index) {
                if (res) {
                    $($btnDelete).parent().parent().remove();
                    top.layer.close(index); //再执行关闭
                }
            });
        });

        //输入框失去焦点
        $("#lstTransfer tbody").find("tr").each(function(index) {
            var $trSettlement4Tr = $("#lstSettlement").children("tbody").find("tr[id='lstTrSettlement4']");
            var $trSettlement4Amount = $trSettlement4Tr.children("td").eq(1).find("input");
            $(this).find("td").eq(3).find("input").on("change", function (e) {
               var value = $(this).val();
               $(this).val($.formatMoney(value, 2));

               var _total = 0;
               $("#lstTransfer tbody").find("tr").each(function(index) {
                   $(this).find("td").eq(3).find("input").each(function(index) {
                      var _value = $(this).val();
                      _total = calc.add(_total, Number(_value));
                   });
               });
               $trSettlement4Amount.val($.formatMoney(_total, 2));
               autoCalcSettlement();
            });
        });
    }

    //设置公务卡结算回调
    reloadCivilServiceCardCallBack = function(data, index) {
        var $tbody = $("#lstCivilServiceCard").find("tbody");
        var $trHtml = "";

        for(var i=0;i<data.length;i++) {
            var item = data[i];
            $trHtml += '<tr class="nk-receipt-list-tr">';
            $trHtml += '    <td class="nk-receipt-list-td" style="width: 80px; text-align: center;">'+item.realName+'</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align:center;">'+item.officialBankAccount+'</td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align:center;">';
            $trHtml += '        <input type="text" class="nk-receipt-input lr-input-wdatepicker" onclick="WdatePicker({dateFmt:\'yyyy-MM-dd\',qsEnabled:false,isShowClear:false,isShowToday:false,onpicked:function(){$(this).trigger(\'change\');}})" style="height: 31px; width: 120px; margin-left: 2px; margin-right: 2px; padding-right: 2px;">';
            $trHtml += '    </td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <input type="text" class="nk-receipt-input" ';if(!!item.amount){ $trHtml +=' value="'+$.formatMoney(item.amount, 2)+'"'}  $trHtml += '  style="width: 100px; text-align: right; margin-right: 0px;">';
            $trHtml += '        <span class="nk-receipt-label" style="width: auto;">元</span></td>';
            $trHtml += '    <td class="nk-receipt-list-td" style="text-align: center;">';
            $trHtml += '        <a href="javascript:void(0);" class="nk-receipt-linkbutton p-del btn-style-del w2" name="btnDeleteCivilServiceCard">删除</a>';
            $trHtml += '    </td>';
            $trHtml += '</tr>';
        }
        $tbody.empty();
        $tbody.append($trHtml);

        //删除行记录
        $("#lstCivilServiceCard a[name='btnDeleteCivilServiceCard']").on("click", function (e) {
            var $btnDelete = this;
            learun.layerConfirm('是否确认删除该项?', function (res, index) {
                if (res) {
                    $($btnDelete).parent().parent().remove();
                    top.layer.close(index); //再执行关闭
                }
            });
        });

        //输入框失去焦点
        $("#lstCivilServiceCard tbody").find("tr").each(function(index) {
            var $trSettlement3Tr = $("#lstSettlement").children("tbody").find("tr[id='lstTrSettlement3']");
            var $trSettlement3Amount = $trSettlement3Tr.children("td").eq(1).find("input");
            $(this).find("td").eq(3).find("input").on("change", function (e) {
               var value = $(this).val();
               $(this).val($.formatMoney(value, 2));

               var _total = 0;
               $("#lstCivilServiceCard tbody").find("tr").each(function(index) {
                   $(this).find("td").eq(3).find("input").each(function(index) {
                      var _value = $(this).val();
                      _total = calc.add(_total, Number(_value));
                   });
               });
               $trSettlement3Amount.val($.formatMoney(_total, 2));
               autoCalcSettlement();
            });
        });
    }

    page.init();
}
